﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using WeChat.EntityFramework;

namespace WeChat.UI
{
    /// <summary>
    /// 由于项目和EF版本不一致 需要使用此类 否则数据迁移会报错
    /// Unable to create an object of type 'WeChatContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
    /// </summary>
    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<WeChatContext>
    {
        public WeChatContext CreateDbContext(string[] args)
        {
            var configBuilder = new ConfigurationBuilder()
               .AddJsonFile("appsettings.json")
               .Build();

            //数据库注入
            var connectionStr = configBuilder.GetConnectionString("WeChatDb");
            var builder = new DbContextOptionsBuilder<WeChatContext>();
            builder.UseSqlServer(connectionStr);
            return new WeChatContext(builder.Options);
        }
    }
}
